﻿using System.Threading;
using System.Collections;
namespace System.Web
{
    public partial class NetHttpApplication
    {
        internal class ThreadContext
        {
            private NetHttpContext _context;
            //private bool _hasLeaveBeenCalled;
            //private ImpersonationContext _impersonationContext;
            //private HttpContext _savedContext;
            //private CultureInfo _savedCulture;
            //private IPrincipal _savedPrincipal;
            //private SynchronizationContext _savedSynchronizationContext;
            //private CultureInfo _savedUICulture;
            //private bool _setThread;
            //private CultureInfo _setThreadCulture;
            //private CultureInfo _setThreadUICulture;

            internal ThreadContext(NetHttpContext context)
            {
                _context = context;
            }

            internal void Enter(bool setImpersonationContext)
            {
                //_savedContext = DisposableHttpContextWrapper.SwitchContext(_context);
                //if (setImpersonationContext)
                //    SetImpersonationContext();
                //_savedSynchronizationContext = AsyncOperationManager.SynchronizationContext;
                //AsyncOperationManager.SynchronizationContext = _context.SyncContext;
                //Guid requestTraceIdentifier = _context.WorkerRequest.RequestTraceIdentifier;
                //if (!(requestTraceIdentifier == Guid.Empty))
                //    CallContext.LogicalSetData("E2ETrace.ActivityID", requestTraceIdentifier);
                //_context.ResetSqlDependencyCookie();
                //_savedPrincipal = Thread.CurrentPrincipal;
                //HttpApplication.SetCurrentPrincipalWithAssert(_context.User);
                //SetRequestLevelCulture(_context);
                //if (_context.CurrentThread == null)
                //{
                //    _setThread = true;
                //    _context.CurrentThread = Thread.CurrentThread;
                //}
            }

            internal void Leave()
            {
                //_hasLeaveBeenCalled = true;
                //if (_setThread)
                //    _context.CurrentThread = null;
                //HttpApplicationFactory.ApplicationState.EnsureUnLock();
                //UndoImpersonationContext();
                //RestoreRequestLevelCulture();
                //AsyncOperationManager.SynchronizationContext = _savedSynchronizationContext;
                //HttpApplication.SetCurrentPrincipalWithAssert(_savedPrincipal);
                //_context.RemoveSqlDependencyCookie();
                //DisposableHttpContextWrapper.SwitchContext(_savedContext);
                //_savedContext = null;
            }

            //private void RestoreRequestLevelCulture()
            //{
            //    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            //    CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
            //    if (_savedCulture != null)
            //    {
            //        if (currentCulture != _savedCulture)
            //        {
            //            HttpRuntime.SetCurrentThreadCultureWithAssert(_savedCulture);
            //            if (_context != null)
            //                _context.DynamicCulture = currentCulture;
            //        }
            //        _savedCulture = null;
            //    }
            //    if (_savedUICulture != null)
            //    {
            //        if (currentUICulture != _savedUICulture)
            //        {
            //            Thread.CurrentThread.CurrentUICulture = _savedUICulture;
            //            if (_context != null)
            //                _context.DynamicUICulture = currentUICulture;
            //        }
            //        _savedUICulture = null;
            //    }
            //}

            //internal void SetImpersonationContext()
            //{
            //    if (_impersonationContext == null)
            //        _impersonationContext = new ClientImpersonationContext(_context);
            //}

            //private void SetRequestLevelCulture(HttpContext context)
            //{
            //    CultureInfo cultureInfo = null;
            //    CultureInfo dynamicUICulture = null;
            //    GlobalizationSection globalization = RuntimeConfig.GetConfig(context).Globalization;
            //    if (!string.IsNullOrEmpty(globalization.Culture))
            //        cultureInfo = context.CultureFromConfig(globalization.Culture, true);
            //    if (!string.IsNullOrEmpty(globalization.UICulture))
            //        dynamicUICulture = context.CultureFromConfig(globalization.UICulture, false);
            //    if (context.DynamicCulture != null)
            //        cultureInfo = context.DynamicCulture;
            //    if (context.DynamicUICulture != null)
            //        dynamicUICulture = context.DynamicUICulture;
            //    Page currentHandler = context.CurrentHandler as Page;
            //    if (currentHandler != null)
            //    {
            //        if (currentHandler.DynamicCulture != null)
            //            cultureInfo = currentHandler.DynamicCulture;
            //        if (currentHandler.DynamicUICulture != null)
            //            dynamicUICulture = currentHandler.DynamicUICulture;
            //    }
            //    _savedCulture = Thread.CurrentThread.CurrentCulture;
            //    _savedUICulture = Thread.CurrentThread.CurrentUICulture;
            //    if ((cultureInfo != null) && (cultureInfo != Thread.CurrentThread.CurrentCulture))
            //    {
            //        HttpRuntime.SetCurrentThreadCultureWithAssert(cultureInfo);
            //        _setThreadCulture = cultureInfo;
            //    }
            //    if ((dynamicUICulture != null) && (dynamicUICulture != Thread.CurrentThread.CurrentUICulture))
            //    {
            //        Thread.CurrentThread.CurrentUICulture = dynamicUICulture;
            //        _setThreadUICulture = dynamicUICulture;
            //    }
            //}

            //internal void Synchronize(HttpContext context)
            //{
            //    context.DynamicCulture = Thread.CurrentThread.CurrentCulture;
            //    context.DynamicUICulture = Thread.CurrentThread.CurrentUICulture;
            //}

            //internal void UndoImpersonationContext()
            //{
            //    if (_impersonationContext != null)
            //    {
            //        _impersonationContext.Undo();
            //        _impersonationContext = null;
            //    }
            //}

            //internal bool HasLeaveBeenCalled
            //{
            //    get { return _hasLeaveBeenCalled; }
            //}
        }
    }
}